<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="公司全称" prop="CompanyAllName">
        <el-input v-model="form.CompanyAllName" />
      </el-form-item>
      <el-form-item label="税号" prop="LicenseCode">
        <el-input v-model="form.LicenseCode" />
      </el-form-item>
      <el-form-item label="地址" prop="LicenseAddress">
        <el-input v-model="form.LicenseAddress" />
      </el-form-item>
      <el-form-item label="电话" prop="LicenseMobile">
        <el-input v-model="form.LicenseMobile" />
      </el-form-item>
      <el-form-item label="开户银行" prop="LicenseDepositBank">
        <el-input v-model="form.LicenseDepositBank" />
      </el-form-item>
      <el-form-item label="银行账号" prop="LicenseBankAccount">
        <el-input v-model="form.LicenseBankAccount" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { EditLicenses } from '@/api/businessOrderCreate'

  export default defineComponent({
    name: 'Invoicing',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          CompanyAllName: [
            { required: true, trigger: 'blur', message: '请输入公司全称' },
          ],
          LicenseCode: [
            { required: true, trigger: 'blur', message: '请输入税号' },
          ],
          LicenseAddress: [
            { required: true, trigger: 'blur', message: '请输入地址' },
          ],
          LicenseMobile: [
            { required: true, trigger: 'blur', message: '请输入电话' },
          ],
          LicenseDepositBank: [
            { required: true, trigger: 'blur', message: '请输入开户银行' },
          ],
          LicenseBankAccount: [
            { required: true, trigger: 'blur', message: '请输入银行账号' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '开票信息'
        } else {
          state.title = '开票信息'
          console.log(row, '12312312312')
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await EditLicenses(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
